package provider;

import java.awt.Point;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class MapleData
        implements MapleDataEntity, Iterable<MapleData> {
    /*  20 */   private static final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    private Node node;
    private File imageDataDir;

    @Override
    public String toString() {
        return "MapleData{" +
                "node=" + node +
                ", imageDataDir=" + imageDataDir +
                '}';
    }

    private MapleData(Node node, File file) {
        /*  25 */
        this.node = node;
        /*  26 */
        this.imageDataDir = file;
    }

    public MapleData(FileInputStream fis, File imageDataDir) {
        try {
            /*  31 */
            this.node = documentBuilderFactory.newDocumentBuilder().parse(fis).getFirstChild();
            /*  32 */
        } catch (ParserConfigurationException | org.xml.sax.SAXException | java.io.IOException e) {
            /*  33 */
            throw new RuntimeException(e);
        }
        /*  35 */
        this.imageDataDir = imageDataDir;
    }

    public MapleData getChildByPath(String path) {
        /*  39 */
        String[] segments = path.split("/");
        /*  40 */
        if (segments[0].equals("..")) {
            /*  41 */
            return ((MapleData) getParent()).getChildByPath(path.substring(path.indexOf("/") + 1));
        }

        /*  44 */
        Node myNode = this.node;
        /*  45 */
        for (String segment : segments) {
            /*  46 */
            NodeList childNodes = myNode.getChildNodes();
            /*  47 */
            boolean foundChild = false;
            /*  48 */
            for (int i = 0; i < childNodes.getLength(); i++) {
                /*  49 */
                Node childNode = childNodes.item(i);
                /*  50 */
                if (childNode != null && childNode.getAttributes() != null && childNode.getAttributes().getNamedItem("name") != null && childNode.getNodeType() == 1 && childNode.getAttributes().getNamedItem("name").getNodeValue().equals(segment)) {
                    /*  51 */
                    myNode = childNode;
                    /*  52 */
                    foundChild = true;
                    break;
                }
            }
            /*  56 */
            if (!foundChild) {
                /*  57 */
                return null;
            }
        }
        /*  60 */
        return new MapleData(myNode, (new File(this.imageDataDir, getName() + "/" + path)).getParentFile());
    }

    public List<MapleData> getChildren() {
        /*  64 */
        List<MapleData> ret = new ArrayList<>();
        /*  65 */
        NodeList childNodes = this.node.getChildNodes();
        /*  66 */
        for (int i = 0; i < childNodes.getLength(); i++) {
            /*  67 */
            Node childNode = childNodes.item(i);
            /*  68 */
            if (childNode != null && childNode.getNodeType() == 1) {
                /*  69 */
                ret.add(new MapleData(childNode, new File(this.imageDataDir, getName())));
            }
        }
        /*  72 */
        return ret;
    }

    public Object getData() {
        /*  76 */
        NamedNodeMap attributes = this.node.getAttributes();
        /*  77 */
        MapleDataType type = getType();
        /*  78 */
        switch (type) {
            case DOUBLE:
                /*  80 */
                return Double.valueOf(Double.parseDouble(attributes.getNamedItem("value").getNodeValue()));

            case FLOAT:
                /*  83 */
                return Float.valueOf(Float.parseFloat(attributes.getNamedItem("value").getNodeValue()));

            case INT:
                if (Long.valueOf(Long.parseLong(attributes.getNamedItem("value").getNodeValue())) > Integer.MAX_VALUE) {

                    return Long.valueOf(Long.parseLong(attributes.getNamedItem("value").getNodeValue()));
                } else {
                    return Integer.valueOf(Integer.parseInt(attributes.getNamedItem("value").getNodeValue()));
                }
            case LONG:
                /*  89 */
                return Long.valueOf(Long.parseLong(attributes.getNamedItem("value").getNodeValue()));

            case SHORT:
                /*  92 */
                return Short.valueOf(Short.parseShort(attributes.getNamedItem("value").getNodeValue()));

            case STRING:
            case UOL:
                /*  96 */
                return attributes.getNamedItem("value").getNodeValue();

            case VECTOR:
                /*  99 */
                return new Point(Integer.parseInt(attributes.getNamedItem("x").getNodeValue()), Integer.parseInt(attributes.getNamedItem("y").getNodeValue()));

            case CANVAS:
                /* 102 */
                return new MapleCanvas(Integer.parseInt(attributes.getNamedItem("width").getNodeValue()), Integer.parseInt(attributes.getNamedItem("height").getNodeValue()), new File(this.imageDataDir, getName() + ".png"));
        }

        /* 105 */
        return null;
    }

    public MapleDataType getType() {
        /* 109 */
        String nodeName = this.node.getNodeName();
        /* 110 */
        switch (nodeName) {
            case "imgdir":
                /* 112 */
                return MapleDataType.PROPERTY;
            case "canvas":
                /* 114 */
                return MapleDataType.CANVAS;
            case "convex":
                /* 116 */
                return MapleDataType.CONVEX;
            case "sound":
                /* 118 */
                return MapleDataType.SOUND;
            case "uol":
                /* 120 */
                return MapleDataType.UOL;
            case "double":
                /* 122 */
                return MapleDataType.DOUBLE;
            case "float":
                /* 124 */
                return MapleDataType.FLOAT;
            case "int":
                /* 126 */
                return MapleDataType.INT;
            case "short":
                /* 128 */
                return MapleDataType.SHORT;
            case "string":
                /* 130 */
                return MapleDataType.STRING;
            case "vector":
                /* 132 */
                return MapleDataType.VECTOR;
            case "null":
                /* 134 */
                return MapleDataType.IMG_0x00;
            case "long":
                /* 136 */
                return MapleDataType.LONG;
        }
        /* 138 */
        return null;
    }


    public MapleDataEntity getParent() {
        /* 143 */
        Node parentNode = this.node.getParentNode();
        /* 144 */
        if (parentNode.getNodeType() == 9) {
            /* 145 */
            return null;
        }
        /* 147 */
        return new MapleData(parentNode, this.imageDataDir.getParentFile());
    }


    public String getName() {
        /* 152 */
        return this.node.getAttributes().getNamedItem("name").getNodeValue();
    }


    public Iterator<MapleData> iterator() {
        /* 157 */
        return getChildren().iterator();
    }
}